(function($) {

	$.fn.main={};
	$.fn.main.data={"num":'1',"sbh":''};
	$.fn.main.initial=function(){
		getSbh();		
		setInterval(updateinfo, 5000);
		
	};
	var bindsbClick=function(){
		$('.cur').on('click',function(){
			var sbh=$(this).find('.t2').html();
			$.fn.main.data['sbh']=sbh;
			updateinfo();
		});
	};
	var updateinfo=function(){
	//	$.fn.main.data={"num":'1',"sbh":'430283183235574605DDFF38'};
		$.ajax({
			url:'args/getLastRecord',
			type:'post',
			contentType:'application/json;charset=utf-8',
			data:$.toJSON($.fn.main.data),
			success:function(data){
				if (data['status']==1001) {
					data=data['body'];
					updateId1(data[0]['gzqd']);
					updateId2(data[0]['kqwd']);
					updateId3(data[0]['kqsd']);
					updateId4(data[0]['trwd']);
					updateId5(data[0]['trsd']);
				}
				else {
					alert('error');
				}
			}
		});
	};
	var updateId1=function(data){
		var attr='{"percent": "'+data+'","size": "150","borderWidth": "4","bgFill": "#cacaca","frFill": "#40f701","textSize": "40","textWeight": "600","textColor": "#42fe00"}';
		$('#diagram-id-1').html('<div class="title" style="width:68px;height:18px;background:#187bc1;color:#fff;border-radius:9px;font-size:15px;line-height:18px;margin-left:4px;">优</div>');
		$('#diagram-id-1').attr('data-circle-diagram',attr);
		$('#diagram-id-1').circleDiagram();
	
	};
	var updateId2=function(data){
		var attr='{"percent": "'+data+'","size": "107","borderWidth": "4","bgFill": "#cacaca","frFill": "#6dc2a6","textSize": "38","textColor": "#6dc2a6"}';
		$('#diagram-id-2').html('<div class="title" style="color:#6dc2a6;"></div> <div class="title1" style="color:#6dc2a6;">温 度℃</div>');
		$('#diagram-id-2').attr('data-circle-diagram',attr);
		$('#diagram-id-2').circleDiagram();
	};
	var updateId3=function(data){
		var attr='{"percent": "'+data+'","size": "107","borderWidth": "4","bgFill": "#cacaca","frFill": "#a6024a","textSize": "38","textColor": "#a6024a"}';
		$('#diagram-id-3').html('<div class="title" style="color:#a6024a;"></div><div class="title1" style="color:#a6024a;">湿 度%</div>');
		$('#diagram-id-3').attr('data-circle-diagram',attr);
		$('#diagram-id-3').circleDiagram();
	};
	var updateId4=function(data){
		var attr='{"percent": "'+data+'","size": "107","borderWidth": "4","bgFill": "#cacaca","frFill": "#a3b401","textSize": "38","textColor": "#a3b401"}';
		$('#diagram-id-4').html('<div class="title" style="color:#a3b401;"></div><div class="title1" style="color:#a3b401;">温 度℃</div>');
		$('#diagram-id-4').attr('data-circle-diagram',attr);
		$('#diagram-id-4').circleDiagram();
	};
	var updateId5=function(data){
		var attr='{"percent": "'+data+'","size": "107","borderWidth": "4","bgFill": "#cacaca","frFill": "#797e00","textSize": "38","textColor": "#797e00"}';
		$('#diagram-id-5').html('<div class="title" style="color:#797e00;"></div><div class="title1" style="color:#797e00;">湿 度%</div>');
		$('#diagram-id-5').attr('data-circle-diagram',attr);
		$('#diagram-id-5').circleDiagram();
	};
	var sbjz=function(){
		$('#sbxz .sbs').remove();
		var sb=$('<a href=#></a>');
		var sbs='<li class="cur"><span class="t1">430283183235574605DDFF38</span><span class="t2">金埔1号检测点</span><span class="t3"><img class="cloud" src="image/cloudonline.png"/>在线</span></li>';
	//	sb.append(sbs);
	//	var sbs0='<li class="cur"><span class="t1">430283183235574605DDFF38</span><span class="t2">金埔2号检测点</span><span class="t3"><img class="cloud" src="image/cloudonline.png"/>在线</span></li>';
	//	sb.append(sbs0);
		var sbs1='<li class="cur"><span class="t1"></span><span class="t2"></span><span class="t3"><img class="cloud" src="image/cloudoffline.png"/>离线</span></li>';
		sb.append(sbs1);
		sb.append(sbs1);
		sb.append(sbs1);
		sb.append(sbs1);
		$('#sbxz').append(sb);
	};
	 var getSbh = function() {
	        $.ajax({
	            url : 'device/getAll',
	            dataType : 'json',
	            success : function(data) {
	                if (data['status'] == 1001) {
	                    data = data['body'];
	                	$('#sbxz .sbs').remove();
	                	$.fn.main.data['sbh']=data[data.length-1]['argid'];
	                    for (var i = data.length-1; i >= 0; i--) {
	                    	getByArgid(data[i]['argid']);
	                    }
	                    updateinfo();
	                    setTimeout(bindsbClick, 3000);
	                }
	            },
	            error:function(){
	            	alert('获取数据失败');
	            }
	        });
	    };
	    
	    var getByArgid=function(argid){
	    	$.ajax({
	    		url:'device/getByargid/'+argid,
	    		dataType:'json',
	    		success:function(data){
	    			if (data['status']==1001) {
						data=data['body'];
						var sb=$('<a href="#"></a>');
						var sbs='<li class="cur"><span class="t1">'+data['name']+'</span><span class="t2">'+argid+'</span><span class="t3"><img class="cloud" src="image/cloudonline.png"/>在线</span></li>';
						sb.append(sbs);
						$('#sbxz').append(sb);
					}
	    		}
	    	});
	    };
})(jQuery);